/*
Copyright 2023 Flant JSC
Licensed under the Deckhouse Platform Enterprise Edition (EE) license. See https://github.com/deckhouse/deckhouse/blob/main/ee/LICENSE
*/

package hooks

import (
	"fmt"

	"github.com/flant/addon-operator/pkg/module_manager/go_hook"
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

	"github.com/deckhouse/deckhouse/dhctl/pkg/config"
	"github.com/deckhouse/deckhouse/dhctl/pkg/infrastructureprovider/cloud/vcd"
	"github.com/deckhouse/deckhouse/go_lib/hooks/cluster_configuration"
)

func preparatorProvider(_ string) config.MetaConfigPreparator {
	return vcd.NewMetaConfigPreparatorWithoutLogger(vcd.MetaConfigPreparatorParams{
		// todo it was bad idea patch metaconfig during installation
		// we need to prepare meta config in dhctl during installation
		// for checking vcd version
		// we do not want to prepare metaconfig here because we already got prepared
		// after installation during first deckhouse converge and legacy mode will get
		// in legacy_mode.go hook with order 10, this hook has 20 order index
		// after first converge we deploy vcd data discoverer and hook legacy_mode.go
		// directory form data discoverer
		PrepareMetaConfig: false,
		// cluster prefix does not provide here
		ValidateClusterPrefix: false,
	})
}

var _ = cluster_configuration.RegisterHook(func(input *go_hook.HookInput, metaCfg *config.MetaConfig, providerDiscoveryData *unstructured.Unstructured, secretFound bool) error {
	if !secretFound {
		return fmt.Errorf("kube-system/d8-provider-cluster-configuration secret not found")
	}
	input.Values.Set("cloudProviderVcd.internal.providerClusterConfiguration", metaCfg.ProviderClusterConfig)
	input.Values.Set("cloudProviderVcd.internal.providerDiscoveryData", providerDiscoveryData.Object)
	return nil
}, cluster_configuration.NewConfig(preparatorProvider))
